package com.carrot.gateway.entity;

import com.carrot.gateway.property.SecurityOAuth2Properties;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * 客户端凭证
 */
@Data
public class ClientCertificate {

    /**
     * 客户端ID
     */
    private String clientId;
    /**
     * 客户端密钥
     */
    private String clientSecret;
    /**
     * 请求的授权范围
     */
    private String scope;
    /**
     * 认证地址
     */
    private String authAddress;


    public static ClientCertificate build(String appName, SecurityOAuth2Properties securityOAuth2Properties){
        ClientCertificate clientCertificate=new ClientCertificate();
        Map<String, String> energyAppletClient = securityOAuth2Properties.getEnergyAppletClient();
        String energyAppletClientId = energyAppletClient.get("client-id");
        if(StringUtils.isNotEmpty(appName)&&appName.equals(energyAppletClientId)){
            clientCertificate.setClientId(energyAppletClientId);
            clientCertificate.setClientSecret(energyAppletClient.get("client-secret"));
         }else {
            //默认为SAAS系统客户端
            Map<String, String> energyClient = securityOAuth2Properties.getEnergyClient();
            clientCertificate.setClientId(energyClient.get("client-id"));
            clientCertificate.setClientSecret(energyClient.get("client-secret"));
        }
        clientCertificate.setScope(securityOAuth2Properties.getScope());
        clientCertificate.setAuthAddress(securityOAuth2Properties.getIssuerUri());
        return clientCertificate;
    }
}
